package com.softline.documentsmall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softline.documentsmall.entity.DictionaryKeywordConfig;
import com.softline.documentsmall.ret.BusinessErrorEnum;
import com.softline.documentsmall.ret.ReturnVO;
import com.softline.documentsmall.ret.ReturnVOUtils;
import com.softline.documentsmall.service.DictionaryKeywordConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 词库关键词权重管理
 *
 * @author zhongweicheng
 * @date 2020/8/25 10:11
 */
@Api(value = "词库关键词权重管理", tags = {"词库关键词权重管理"})
@RestController
@RequestMapping("/dictionaryKeywordConfig")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
@Slf4j
public class DictionaryKeywordConfigController {

    @Autowired
    DictionaryKeywordConfigService service;

    @ApiOperation(value = "列表展示", notes = "列表展示")
    @GetMapping(value = "/list")
    public ReturnVO list(@RequestParam(name = "id", required = false) Long id, @RequestParam(name = "dictionaryId", required = false) Integer dictionaryId, @RequestParam(name = "keyword", required = false) String keyword, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<DictionaryKeywordConfig> result = new Page<>();
        List<DictionaryKeywordConfig> records = service.searchListByParams(id, dictionaryId, keyword, pageNo, pageSize);
        Long count = service.getTotalCount(id, dictionaryId, keyword);
        result.setRecords(records);
        result.setCurrent(pageNo);
        result.setSize(pageSize);
        result.setTotal(count);
        return ReturnVOUtils.success(result);
    }

    @ApiOperation(value = "根据id更新", notes = "根据id更新")
    @PutMapping(value = "/update")
    public ReturnVO updateById(DictionaryKeywordConfig dictionaryKeywordConfig) {
        if (dictionaryKeywordConfig == null || dictionaryKeywordConfig.getId() == null) {
            return ReturnVOUtils.businessFail(BusinessErrorEnum.PARAMS_HAS_NULL);
        }
        //根据id更新
        try {
            service.updateById(dictionaryKeywordConfig);
        } catch (Exception e) {
            log.warn("DictionaryKeywordConfigController updateById exception:{}", e.getMessage());
            return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);
        }
        return ReturnVOUtils.success();
    }

    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    @DeleteMapping(value = "/deleteById")
    public ReturnVO deleteById(@RequestParam(name = "id") Long id) {
        try {
            service.removeById(id);
        } catch (Exception e) {
            log.warn("DictionaryKeywordConfigController deleteById exception:{}", e.getMessage());
            return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);
        }
        return ReturnVOUtils.success();
    }

    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping(value = "/add")
    public ReturnVO add(DictionaryKeywordConfig dictionaryKeywordConfig) {

        try {
            service.save(dictionaryKeywordConfig);
        } catch (Exception e) {
            log.warn("DictionaryKeywordConfigController add exception:{}", e.getMessage());
            return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);
        }
        return ReturnVOUtils.success();
    }

    @ApiOperation(value = "关键词更新", notes = "关键词更新")
    @GetMapping(value = "/keywordUpdate")
    public ReturnVO keywordUpdate() {
        try {
            service.dictionaryKeywordUpdate();
        } catch (Exception e) {
            log.error("DictionaryKeywordConfigController add exception:{}", e.getMessage());
        }
        return ReturnVOUtils.success();
    }

}
